package com.test.burnincash.aaahpoppomelo.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;

public class OrderDownloadActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    private TextView downloadTv,downloadedTv;
    private View downloadLineView,downloadedLineView;
    private LinearLayout downloadGroupLayout,downloadedGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_download_hpoppomelo);
        init();
    }

    private void init(){

        setBackBtn_burnin(findViewById(R.id.activity_order_download_ib_back));

        downloadTv = findViewById(R.id.activity_order_download_tv_download);
        downloadedTv = findViewById(R.id.activity_order_download_tv_downloaded);
        downloadLineView = findViewById(R.id.activity_order_download_view_download_line);
        downloadedLineView = findViewById(R.id.activity_order_download_view_downloaded_line);
        downloadGroupLayout = findViewById(R.id.activity_order_download_layout_download_group);
        downloadedGroupLayout = findViewById(R.id.activity_order_download_layout_downloaded_group);

        downloadTv.setOnClickListener(v -> changeToDownload());
        downloadedTv.setOnClickListener(v -> changeToDownloaded());

        updateOrders();
    }

    private void changeToDownload(){

        downloadTv.setTextColor(Color.parseColor("#FF276AE4"));
        downloadedTv.setTextColor(Color.parseColor("#B3292D32"));

        downloadLineView.setVisibility(View.VISIBLE);
        downloadedLineView.setVisibility(View.INVISIBLE);

        downloadGroupLayout.setVisibility(View.VISIBLE);
        downloadedGroupLayout.setVisibility(View.GONE);

    }

    private void changeToDownloaded(){
        downloadTv.setTextColor(Color.parseColor("#B3292D32"));
        downloadedTv.setTextColor(Color.parseColor("#FF276AE4"));

        downloadLineView.setVisibility(View.INVISIBLE);
        downloadedLineView.setVisibility(View.VISIBLE);

        downloadGroupLayout.setVisibility(View.GONE);
        downloadedGroupLayout.setVisibility(View.VISIBLE);
    }

    private void updateOrders(){

        downloadGroupLayout.removeAllViews();
        downloadedGroupLayout.removeAllViews();

        for (Order_hpoppomelo order: UserData_hpoppomelo.getInstance().getOrders()){
            showOrder(order);
        }

    }

    @SuppressLint("SetTextI18n")
    private void showOrder(Order_hpoppomelo order){

        @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.item_order_hpoppomelo,null);

        TextView orderNumTv = view.findViewById(R.id.view_order_tv_order_num);
        Button downloadBtn = view.findViewById(R.id.view_order_btn);
        TextView dateTv = view.findViewById(R.id.view_order_tv_date);
        TextView customerTv = view.findViewById(R.id.view_order_tv_customer);

        orderNumTv.setText(String.valueOf(order.getOrderNumber()));
        dateTv.setText(order.getDate());
        customerTv.setText(order.getCustomer());

        if (order.isDownloaded()){
            downloadBtn.setText("Downloaded");
            downloadBtn.setBackgroundResource(R.drawable.ic_rc_blue_25dp);
            downloadBtn.setTextColor(Color.WHITE);
            downloadBtn.setOnClickListener(null);
            downloadedGroupLayout.addView(view);
        }else {
            downloadBtn.setText("Download");
            downloadBtn.setOnClickListener(v -> {
                updateOrders();
            });
            downloadGroupLayout.addView(view);
        }

    }



}